/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager;

import br.com.foxline.enums.PartQuality;
import br.com.foxline.enums.TypeDocument;
import br.com.foxline.enums.TypeTitleDocument;
import br.com.foxline.model.*;
import br.com.foxline.service.*;
import br.com.foxline.util.ConverterImg;
import br.com.foxline.util.Msg;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerTitleDocument implements Serializable {

    @EJB
    StampService stampService;
    @EJB
    PeopleService peopleService;
    @EJB
    QualityService qualityService;
    @EJB
    TypeStampService typeStampService;
    @EJB
    TitleDocumentService titleDocumentService;
    
    private TitleDocument titleDocument;
    private People people;
    private Part part;
    private List<Part> parts;
    private Stamp stamp;
    private List<Stamp> stamps;
    private TypeStamp typeStampObj;
    private Annex annex;
    private List<Annex> annexs;

    @PostConstruct
    public void init() {
        
        people = new People();
        people.setTipoDocumento(TypeDocument.CPF);
        
        part = new Part();
        parts = new ArrayList<Part>();
        stamp = new Stamp();
        stamps = new ArrayList<Stamp>();

        typeStampObj = new TypeStamp();
        annexs = new ArrayList<Annex>();

        titleDocument = new TitleDocument();
    }

    public Annex getAnnex() {
        return annex;
    }

    public void setAnnex(Annex annex) {
        this.annex = annex;
    }

    public List<Annex> getAnnexs() {
        return annexs;
    }

    public void setAnnexs(List<Annex> annexs) {
        this.annexs = annexs;
    }

    public Part getPart() {
        return part;
    }

    public void setPart(Part part) {
        this.part = part;
    }

    public List<Part> getParts() {
        return parts;
    }

    public void setParts(List<Part> parts) {
        this.parts = parts;
    }

    public People getPeople() {
        return people;
    }

    public void setPeople(People people) {
        this.people = people;
    }

    public PeopleService getPeopleService() {
        return peopleService;
    }

    public void setPeopleService(PeopleService peopleService) {
        this.peopleService = peopleService;
    }

    public QualityService getQualityService() {
        return qualityService;
    }

    public void setQualityService(QualityService qualityService) {
        this.qualityService = qualityService;
    }

    public Stamp getStamp() {
        return stamp;
    }

    public void setStamp(Stamp stamp) {
        this.stamp = stamp;
    }

    public StampService getStampService() {
        return stampService;
    }

    public void setStampService(StampService stampService) {
        this.stampService = stampService;
    }

    public List<Stamp> getStamps() {
        return stamps;
    }

    public void setStamps(List<Stamp> stamps) {
        this.stamps = stamps;
    }

    public TitleDocument getTitleDocument() {
        return titleDocument;
    }

    public void setTitleDocument(TitleDocument titleDocument) {
        this.titleDocument = titleDocument;
    }

    public TypeStamp getTypeStampObj() {
        return typeStampObj;
    }

    public void setTypeStampObj(TypeStamp typeStampObj) {
        this.typeStampObj = typeStampObj;
    }

    public TypeStampService getTypeStampService() {
        return typeStampService;
    }

    public void setTypeStampService(TypeStampService typeStampService) {
        this.typeStampService = typeStampService;
    }

    public List<SelectItem> typeTitleDocumentList() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (TypeTitleDocument tipo : TypeTitleDocument.values()) {
                items.add(new SelectItem(tipo, tipo.getName()));
        }
        return items;
    }
    
    public void addParts() {

        if (people.getName().isEmpty() || people.getName() == null) {
            Msg.messagemError("O nome da parte é obrigatório");
            return;
        }

        if (part.getPartQuality() != null) {
            part.setPartQuality(qualityService.find(part.getPartQuality().getId()));
        }

        part.setPeople(people);
        parts.add(part);
        people = new People();
        people.setTipoDocumento(TypeDocument.CPF);
        part = new Part();

    }

    public void remove(int p) {
        parts.remove(p);
    }

    public void addStamps() {
        stamp.setTypeStamp(typeStampService.find(typeStampObj.getId()));
        stamps.add(stamp);
        stamp = new Stamp();
    }

    public void handle(FileUploadEvent event) throws IOException {
        handleAux(event.getFile().getContents(), event.getFile().getFileName(),
                event.getFile().getContentType(), event.getFile().getInputstream());
    }

    private void handleAux(byte[] imageBytes, String nome, String tipo, InputStream is) throws IOException {

        byte[] bytes = null;

        annex = new Annex();
        if (tipo.equalsIgnoreCase("image/tiff")) {
            bytes = ConverterImg.convertTiffToJpg(is);
            annex.setType("jpeg");
            annex.setDescription(nome.replaceAll("tif|tiff", "jpeg"));
        } else {
            bytes = imageBytes;
            annex.setType(tipo);
            annex.setDescription(nome);
        }
        if (bytes != null) {
            annex.setFileByte(bytes);
            annexs.add(annex);
            System.err.println("Annexoo..." + annexs.size());
            annex = new Annex();
            Msg.messagemWarn("Anexado");
        }

    }

    public void ajaxVerifyPeopleExist() {
        String doc = "";
        if (people.getTipoDocumento() == TypeDocument.CNPJ) {
            doc = people.getCnpj();
        } else if (people.getTipoDocumento() == TypeDocument.CPF) {
            doc = people.getCpf();
        } else if (people.getTipoDocumento() == TypeDocument.OAB) {
            doc = people.getOAB();
        }
        if (doc == null || doc.isEmpty()) {
            return;
        }
        People pe = peopleService.existPeople(people.getTipoDocumento(), doc);
        if (pe != null) {
            people = pe;

            if (people.getCnpj() != null) {
                people.setTipoDocumento(TypeDocument.CNPJ);
            } else if (people.getCpf() != null) {
                people.setTipoDocumento(TypeDocument.CPF);
            } else if (people.getOAB() != null) {
                people.setTipoDocumento(TypeDocument.OAB);
            }
        }
    }

    public List<SelectItem> typeDocumentList() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (TypeDocument tipo : TypeDocument.values()) {
            if (tipo != TypeDocument.OAB) {
                items.add(new SelectItem(tipo.getType(), tipo.getName()));
            }
        }
        return items;
    }

    public List<SelectItem> partQualitytList() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (PartQuality tipo : PartQuality.values()) {
            items.add(new SelectItem(tipo));
        }
        return items;
    }
    
     public List<Quality> autocompleteQuality(String str) {
        return qualityService.autoComplete(str);
    }
     
      public void create() {
        System.err.println("CRIANDO TITULO E DOCUMENTOS");
        
        titleDocument.setAnnex(annexs);
        titleDocument.setStamp(stamps);
        People person = null;
        for (Part p : parts) {
            if (p.getPeople().getId() == null) {
                peopleService.save(p.getPeople());
            } else {
                peopleService.update(p.getPeople());
            }

        }
        titleDocument.setDatetcad(new Date());
        titleDocument.setParts(parts);
        titleDocumentService.save(titleDocument);
        init();

    }
}
